package com.jy.servlet;

import com.alibaba.fastjson.JSON;
import com.jy.bean.Consignee;
import com.jy.bean.Order;
import com.jy.bean.Orderdetail;
import com.jy.bean.User;
import com.jy.dao.ConsigneeDao;
import com.jy.dao.UserDao;
import com.jy.service.ConsigneeService;
import com.jy.service.Orderservice;
import com.jy.service.UserService;
import com.jy.service.impl.ConsigneeSericelmpl;
import com.jy.service.impl.OrderSericelmpl;
import com.jy.service.impl.UserServicelmpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class merchantServlet extends HttpServlet {
    private static final Logger log = LoggerFactory.getLogger(merchantServlet.class);
    private UserService userService;
    private ConsigneeService consigneeService;
    private static Orderservice orderservice;

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        selectAll(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //selectAll(request, response);
        User params = getParamesByRequest(request);
        UserService userService = new UserServicelmpl();

        List<User> userList = userService.selectByParams(params);

        String data = JSON.toJSONString(userList);

        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.println(data);
        log.debug("data:" +data);
    }

    /**
     * 通过请求对象获取参数并封装
     * @param request
     * @return
     */
    private User getParamesByRequest(HttpServletRequest request) {
        User params = new User();
        String user_id = request.getParameter("user_id");
        String user_account = request.getParameter("user_name");
        String user_email = request.getParameter("Emailbox");
        String user_data = request.getParameter("recordDate");
        if (user_id != null && user_id.trim().length()>0){
            params.setId(Integer.parseInt(user_id));
        }
        if (user_account != null && user_account.trim().length()>0){
            params.setName(user_account);
        }
        if (user_email != null && user_email.trim().length()>0){
            params.setEmail(user_email);
        }
        if (user_data != null && user_data.trim().length()>0){
            params.setDatetime(user_data);
        }
        log.debug("params:" + params);
        return params;
    }

    private void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.debug("in selectAll...");
        selectUser(request,response);
        selectConsignee(request,response);
        selectquantity(request,response);
        request.getRequestDispatcher("/merchant.jsp").forward(request,response);
        log.debug("登录成功");
    }

    private void selectquantity(HttpServletRequest request, HttpServletResponse response) {
        log.debug("进入了selectquantity");
        consigneeService = new ConsigneeSericelmpl();
        orderservice = new OrderSericelmpl();
        int count = consigneeService.selectquantity();
        int turnover = orderservice.selectturnover();
        int commodity = consigneeService.selectcommodity();
        List<Orderdetail> orderlies = orderservice.consigneeList();
        log.debug("打印所有订单信息" + orderlies);
        HttpSession session = request.getSession();
        //将查询到的今日订单通过会话发送到merchant页面
        session.setAttribute("count",count);
        //将查询到的今日流水通过会话发送到merchant页面
        session.setAttribute("turnover",turnover);
        //将查询到的所有商品通过会话发送到merchant页面
        session.setAttribute("commodity",commodity);
        //将查询到的所有订单通过会话发送到merchant页面
        session.setAttribute("orderList",orderlies);
    }

    private void selectUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        log.debug("进入了selectUser");
        userService = new UserServicelmpl();
        List<User> users = userService.selectuser();
        HttpSession session = request.getSession();
        //将查询到的用户信息通过会话发送到merchant页面
        session.setAttribute("Listuser", users);
    }

    private void selectConsignee(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.debug("进入了selectConsignee");
        consigneeService = new ConsigneeSericelmpl();
        HttpSession session = request.getSession();
        List<Consignee> consignees= consigneeService.consigneeList();
        //将查询到所有商品信息通过会话发送到merchant页面
        session.setAttribute("Listconsignee", consignees);
    }

    public static void main(String[] args) {
        List<Orderdetail> orderlies = orderservice.consigneeList();
        log.debug("打印所有订单信息" + orderlies);
    }
}
